#!/bin/bash
export PATH=$PATH:$HOME/i2/bin
CUR_DIR=$(dirname `pwd`/${BASH_SOURCE[0]})

export EDITOR=vi
bashenv=${CUR_DIR}bashenv
[ -f $CUR_DIR/bashenv ] && source $bashenv 
_info () {
    echo -e "\e[32m$@\e[39m"
}
_warn () {
    echo -e "\e[93m$@\e[39m"
}
_err () {
    echo -e "\e[31m$@\e[39m"
}

alias ya="yazi "
alias ofzf=$(which fzf)

fzf(){
	#--preview 'cat {}' --preview-window right:30%:hidden
    ofzf --height 95% --preview-window right:90%  --layout=reverse -e -i  "$@"
}

ipz(){
  	local ips="$(ifconfig | grep -E "inet " | grep -v 127 | 	awk '{print $2}')"
	echo "Local Ip: "$ips
    #[ ! -z $(which jq) ] && (curl "https://api.live.bilibili.com/client/v1/Ip/getInfoNew"|jq)
    #echo `curl -s http://ipecho.net/plain`
    /usr/bin/curl -qSs https://api.live.bilibili.com/client/v1/Ip/getInfoNew|jq ".data"
    _info "dns server:"
}

u(){
    local is_local=$1
    [  ! -z "$is_local" ] && {
        git add . 
        git commit -am 'n'
        git push origin master
        git push origin main
    }
    cd $HOME

	if [ ! -d $HOME/i2 ];then
        git clone https://gitee.com/ifa6/i2.git
    else
        cd i2
        git reset --hard
        git pull origin --allow-unrelated-histories
    fi
    [ ! -z $HOME/.bashrc ] && touch $HOME/.bashrc && echo '#!/bin/bash' >> $HOME/.bashrc
    if grep -q "source $HOME/i2/bashrc.sh" $HOME/.bashrc;then
        echo "exists"
    else
        echo "source $HOME/i2/bashrc.sh" >> $HOME/.bashrc
    fi
    source $HOME/i2/bashrc.sh
    cp -Rf $HOME/i2/ssh_config ~/.ssh/config
    cp -Rf $HOME/i2/ssh_config ~/.ssh/config
    chmod -R 777 $HOME/i2/bin

    sed  -i  -e '/UseKeychain/'d  ~/.ssh/config
}
pz(){
	nmap -n ${host:-localhost}
}
cjz(){
    curl -H 'Content-Type: application/json' $@
}
cpz(){
  _info "代理请求,请注意 v rp 打开通道"
  curl socks4a://localhost:10080 "$@"
}  
function l(){
    local file=$(fzf)
    local action=$(echo -e "${OP_ACTION:-"vi,cat"}"|tr ',' '\n'|fzf)
    $action $file
}
# scz(){
#     curl socks5://$1 "${@:1}"
# }
# shz(){
#     curl https://$1 "${@:1}"
# }
f(){
    fd -I -H $@|fzf
}


fdz(){
   fd -I -H  -t d $@|fzf
}
ffz(){
   fd -I -H  -t f $@|fzf
}
sl(){
    local kw="$1"
    local hosts="$(grep "^ *Host .*$kw" $HOME/i2/ssh_hosts)"
    local len=$(echo -e "$hosts"|wc -l)
    local choice=""
    if [ "$len" -gt "1" ];then
        choice="$(echo -e "$hosts"|fzf|awk '{print $2}'|tr -d ' ')"
        _info "$choice choice"
    else
        choice="$(echo -e "$hosts"|awk '{print $2}'|tr -d ' ')"
    fi
    [ ! -z "$choice" ] && {

        _info "选择登录的主机: $choice"
        export CUR_SSH_HOST="$choice"
        [ -z "$NO_SSH_LOGIN" ] && {
            ssh $choice
        }
    }
}
rpz(){
    local remote_host=${2:-ajp}
    local re_port=${1:-8022}
    local remote_port=${3:-$re_port}
    _info "ssh 端口转发 $remote_host:$remote_port 到 $re_port"
    ssh -f -N -R $remote_port:localhost:$re_port $remote_host
}
pkz(){
    _info "kill process"
    local pid=$(ps -ef|fzf|awk '{print $2}')
    _info "kill $pid"
    [ ! -z "$pid" ] && kill $pid
    sleep 1
    _info "find process with $pid"
    ps -ef|grep "$pid"
}
ngrz(){
    nginx -s stop
    nginx
}
plz(){
    ps -eo pid,comm,args,lstart
}
h(){
 
    _info "函数"
    cat -<<EOF
# 所有都带z后缀,z容输入
ngrz 重启ng
plz 列出进程
pkz 列出并杀掉进程
ipz ip信息
cjz json请求
pz 端口扫描
u 更新
f fd搜索
fdz 目录搜索
ffz 文件搜索
sl 选择登录的主机
v 其他命令
cpz socks5代理请求
dz 删除当前目录下
dfz 从某目录开始查找删除
c  进入目录
mz 重命名
EOF

    _info "alias"
    alias 

}
c(){
    local dir=${1:-.}
    cd $(ls -d $dir/*/|fzf)
}
dz(){
    local dir=${1:-.}
    local files="$(ls $dir|fzf -m)"
    _info "删除文件吗 $files :(不空:删)"
    read ans
    [ ! -z "$ans" ] && {
        rm -Rf $files
    }
}
dfz(){
    local dir=${1:-.}
    cd $dir
    local files="$(fzf -m)"
     _info "删除文件吗 $files :(不空:删)"
    read ans
    [ ! -z "$ans" ] && {
        rm -Rf $files
    }
}
mz(){
    local dir=${1:-.}
    cd $dir
    local file=$(fzf)
    local dir=$(dirname "$file")
    _info "新名字:"
    read new_name
    [ ! -z "$new_name" ] && {
        mv "$file" "$dir/$new_name"
    }
    
}
alias iz="pkg install "
alias uiz="pkg install "
alias cz="curl "
alias gcz="git clone "
alias gpz="git pull -u origin --allow-unrelated-histories"
alias gpmz="git pull -u origin main:main --allow-unrelated-histories;git pull -u origin master:master --allow-unrelated-histories"
alias gpuz="git push -u origin main;git push -u origin master;"
alias cls="clear"


